package com.tma.umedia;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Stack;

import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Intent;

public class FolderActivity extends Activity{

	ListView list;
    FolderAdapter adapter;
    Stack<File> history;
	private ArrayList<HashMap<String,String>> items = null;
	private ArrayList<Song> songsList = new ArrayList<Song>();
	private Utilities utils;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_folder);
        
        songsList = this.getIntent().getParcelableArrayListExtra("playlist");
        
        utils = new Utilities();
        history = new Stack<File>();
		list = (ListView) findViewById(R.id.list_dir);
		getFiles(new File("/"));
        
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int selectedRow = (int)id;
		        if(selectedRow == 0){
		            backDir();
		        }else{
		            File file = new File(items.get(selectedRow).get("path"));
		            if(file.isDirectory()){
		                getFiles(file);
		            }else{
		            }
		        }
			}
		});
    }
    
    private void getFiles(File f){
    	history.push(f);
    	File[] files = f.listFiles();
        items = new ArrayList<HashMap<String, String>>();
    	HashMap<String, String> r = new HashMap<String, String>();
    	r.put("name", getString(R.string.previous_folder));
    	r.put("path", getString(R.string.previous_folder));
        items.add(r);
        for(File file : files){
        	HashMap<String, String> i = new HashMap<String, String>();
        	i.put("name", file.getName());
        	i.put("path", file.getPath());
            items.add(i);
        }
        
        Collections.sort( items, new Comparator<HashMap<String, String>>() { 
        	public int compare(HashMap<String, String> file1, HashMap<String, String> file2) {
        		return String.valueOf(file1.get("name")).compareTo(file2.get("name"));
        	} 
        });
        adapter = new FolderAdapter(this, items);
        list.setAdapter(adapter);
    }
    
    private void backDir(){
    	history.pop(); //remove current directory
    	
    	if(history.empty()){ 
    		
    	} else {
		   File selected = history.pop(); //Get the File on Top of history
		   getFiles(selected);
    	}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_folder, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// Starting new intent
		Intent in = new Intent(getApplicationContext(), HomeActivity.class);		
		// Sending songIndex to PlayerActivity
		ArrayList<Song> sendlist = new ArrayList<Song>();
        switch (item.getItemId()) {
            case R.id.ok:
				songsList = utils.getPlayList(history.pop().getPath());
                break;
            case R.id.cancel:
                break;
        }
		for(int i=0; i < songsList.size(); i++){
			sendlist.add(songsList.get(i));
		}
		in.putParcelableArrayListExtra("playlist", sendlist);
		
		// starting new activity
		startActivityForResult(in, 100);
        return true;
    }
}
